package com.spyx.test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo5 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
      //1.创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        //2.执行异步任务
        //任务1
        CompletableFuture<Integer> a1 = CompletableFuture.supplyAsync(() -> {
            int a = 100;
            System.out.println("线程1:" + a);
            return a;
        }, executorService);

        //任务2
        CompletableFuture<Integer> a2 = a1.thenApplyAsync((a) -> {
            int b = 20;
            System.out.println("线程2=" + b);
            return b + a;
        }, executorService);
        //任务3
        CompletableFuture<Integer> a3 = a2.thenApplyAsync((c) -> {
            System.out.println("线程3=" + c);
            return c + 100;
        }, executorService);

        //汇总各个线程完成的结果
        CompletableFuture<Void> future = CompletableFuture.allOf(a1, a2, a3);
        future.get();
        System.out.println("程序结束");


    }
}
